#include <stdlib.h>
#include <stdio.h>
#include <mpi.h>
#include <math.h>
#include <string.h>

int arr[1005][1005];

/***** Begin *****/
int main(int argc, char* argv[]){
    int rank, size, n;
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    int data_buff[2] = {0};
    MPI_Status status;
    if(rank == 0){
        scanf("%d", &n);
        for(int i = 0; i < n; i++){
            arr[i][0] = arr[i][i] = 1;
            for(int j = 1; j <= i - 1; j += size - 1){
                for(int rk = 1; rk < size; rk++){
                    if(j + rk - 1 > i - 1)
                        break;
                    data_buff[0] = arr[i - 1][j + rk - 1 - 1];
                    data_buff[1] = arr[i - 1][j + rk - 1];
                    // printf("send out arr[%d][%d] = %d, arr[%d][%d] = %d\n", i, j + rk - 2, arr[i - 1][j + rk - 1 - 1], i, j + rk - 1, arr[i - 1][j + rk - 1]);
                    MPI_Send(data_buff, 2, MPI_INT, rk, 0, MPI_COMM_WORLD);
                }// 循环利用从进程，向从进程分发数据
                for(int rk = 1; rk < size; rk++){
                    if(j + rk - 1 > i - 1)
                        break;
                    MPI_Recv(data_buff, 1, MPI_INT, rk, 1, MPI_COMM_WORLD, &status);
                    // printf("computing arr[%d][%d], recieve value %d\n", i, j + rk - 1, ans);
                    arr[i][j + rk - 1] = data_buff[0];
                }// 循环利用从进程，接收从进程返回的数据
            }
        }
        for(int rk = 1; rk < size; rk++){
            MPI_Send(data_buff, 1, MPI_INT, rk, 2, MPI_COMM_WORLD);
        }
        printf("[");
        for(int i = 0; i < n; i++){
            if(i != 0)
                printf(",");
            printf("[");
            for(int j = 0; j < i; j++){
                printf("%d,", arr[i][j]);
            }
            printf("%d]", arr[i][i]);
        }
        printf("]");
    }
    else{
        while(1){
            MPI_Recv(data_buff, 2, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
            if(status.MPI_TAG == 2)
                break;
            data_buff[0] += data_buff[1];
            MPI_Send(data_buff, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
        }
    }
    MPI_Finalize();
	return 0;
}
/***** End *****/